<?php

namespace Modules\General;

/**
 * Description of Index
 *
 * @author Admin
 */
class Index extends \Core\Controller
{
	
	public function indexAction()
	{
		$this->_layout->meta()->title(_('Последние добавленые карточки'));
		
		$this->rowset = \App::model('Vocabulary')->getRandomCart(9);
		
		$this->render();
		$this->render('left', 'right');
	}
	
	public function randomWordAction()
	{
		$row = \App::model('Word')->getRandomWord();
		
		echo json_encode($row); exit;
	}
	
	public function addWordAction()
	{
		$row = \App::model('Word')->getByWord($this->_getParam('word'));

		$data = $this->_getPost();
		//file_put_contents(APPLICATION_PATH . '/log.txt', json_encode($data) );
		if(!$row && !empty($data['word']) && !empty($data['translate']))
		{
			$data['word'] = mb_strtolower(trim($data['word']));
			$data['translate'] = $data['translate'];
			
			$data['vocabulary_id'] = 1;
			$data['user_id'] = 1;
			$data['lang'] = 'uk';
	//file_put_contents(APPLICATION_PATH . '/log.txt', json_encode($data));
			$itemId = \App::model('Word')->addWord($data);

			echo json_encode(array('success' => (boolean)$itemId)); exit;
		}
		else
		{
			echo json_encode(array('success' => false)); exit;
		}
	}
	
	public function cardByIdAction()
	{
		$this->_layout->meta()->title(_('Карточка'));
		
		$this->row = \App::model('Vocabulary')->getById($this->getIdByUrl());
		
		if(!$this->row)
			return $this->_404();
		
		$this->_layout->meta()->title($this->row['title']);
		
		$this->render('card-by-id');
		$this->render('left', 'right');
	}
	
	public function notfoundAction()
	{
		echo _('Гуд'); exit;
		
		$this->render();
	}
	
}
